package com.glsc.ngateway.common.base.repo.mysql.cmdb;

import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbDeviceStandard;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface CmdbDeviceStandardRepository extends JpaRepository<CmdbDeviceStandard, Integer>, JpaSpecificationExecutor<CmdbDeviceStandard> {
    List<CmdbDeviceStandard> findAllByIdIn(List<Long> id);

    @Query(value = "select d from CmdbDeviceStandard d where d.isDelete = 0 " +
            "and (:#{#paramSearchDto.deviceStandardCode} is null OR d.deviceStandardCode LIKE CONCAT('%', :#{#paramSearchDto.deviceStandardCode},'%') ) " +
            "and (:#{#paramSearchDto.brand} is null OR d.brand = :#{#paramSearchDto.brand}) " +
            "and (:#{#paramSearchDto.model} is null OR d.model LIKE CONCAT('%', :#{#paramSearchDto.model},'%') ) " +
            "and (:#{#paramSearchDto.uAmount} is null OR d.uAmount = :#{#paramSearchDto.uAmount}) " +
            "and (:#{#paramSearchDto.cpuPortAmount} is null OR d.cpuPortAmount = :#{#paramSearchDto.cpuPortAmount}) " +
            "and (:#{#paramSearchDto.memoryPortAmount} is null OR d.memoryPortAmount = :#{#paramSearchDto.memoryPortAmount}) " +
            "and (:#{#paramSearchDto.diskPortAmount} is null OR d.diskPortAmount = :#{#paramSearchDto.diskPortAmount}) " +
            "and (:#{#paramSearchDto.pciPortAmount} is null OR d.pciPortAmount = :#{#paramSearchDto.pciPortAmount}) " +
            "and (:#{#paramSearchDto.elePortAmount} is null OR d.elePortAmount = :#{#paramSearchDto.elePortAmount}) " +
            "and (:#{#paramSearchDto.standardName} is null OR d.standardName LIKE CONCAT('%', :#{#paramSearchDto.standardName},'%') ) ")
    Page<CmdbDeviceStandard> searchByParam(Pageable pageable, CmdbDeviceStandard paramSearchDto);

    @Transactional
    @Modifying
    @Query(value = "update t_cmdb_device_standard set is_delete = :deleted where device_standard_code in :codes ", nativeQuery = true)
    void updateIsDeleteByCodes(Integer deleted, List<String> codes);

    CmdbDeviceStandard findOneByDeviceStandardCode(String deviceStandardCode);

    List<CmdbDeviceStandard> findAllByStandardNameAndIsDelete(String standardName, Integer noDelete);
}

